package com.letv.app.ljodfh.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jiongbull.jlog.JLog;
import com.letv.app.ljodfh.R;
import com.letv.app.ljodfh.beans.FChannel;
import com.letv.app.ljodfh.ui.adapter.holder.HomeTvChannelHolder;
import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.ListUtils;

import java.util.List;

/**
 * Created by Liuyu on 2018/3/23.
 */

public class HomeTVChannelAdapter extends BaseHolderAbsAdapter<FChannel, HomeTvChannelHolder> {

    private String selectId;

    private int selectPos = -1;

    private OnItemInternalClick defItemClidk = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            FChannel fc = getItem(position);
            if (fc != null) {
                setSelectId(fc.getChannelId(), position);
            }
            if (itemInternalClick != null) {
                itemInternalClick.onItemInternalClick(itemView, view, position);
            }
        }
    };

    public HomeTVChannelAdapter(Context context, List<FChannel> list) {
        super(context, list);
        canSelector = true;
    }

    @Override
    public HomeTvChannelHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item_home_tv_channels, parent, false);
        return new HomeTvChannelHolder(context, v).setInternalClick(defItemClidk);
    }

    @Override
    protected boolean isSelected(FChannel fChannel, int position) {
        JLog.e("oopo  select id=" + selectId + "  pos=" + position);
        return fChannel != null && TextUtils.equals(fChannel.getChannelId(), selectId);
//        return super.isSelected(fChannel, position);
    }

    public HomeTVChannelAdapter setSelectId(String selectId, int selectPos) {
        this.selectId = selectId;
        if (canUseInPos(selectPos, getItemCount())) {
            int old = this.selectPos;
            this.selectPos = selectPos;
            if (old != selectPos) {
                notifyItemChanged(selectPos);
                if (canUseInPos(old, getItemCount())) {
                    notifyItemChanged(old);
                }
            }
        }
        return this;
    }

    private boolean canUseInPos(int i, int size) {
        return i >= 0 && i < size;
    }

}
